package com.iraeta.service.etl;

import cn.hutool.core.util.StrUtil;
import com.iraeta.dao.erp.CurrentStockDao;
import com.iraeta.dao.main.ErpCurrentStoreDao;
import com.iraeta.dao.main.EtllogDao;
import com.iraeta.entity.main.ErpCurrentStore;
import com.iraeta.entity.main.Etllog;
import com.iraeta.uitl.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

@Service
public class ErpEtlService {
    private static final Logger logger = LoggerFactory.getLogger(ErpEtlService.class);
    @Autowired
    ErpCurrentStoreDao erpCurrentStoreDao;
    @Autowired
    CurrentStockDao currentStockDao;
    @Autowired
    EtllogDao etllogDao;

    public Etllog processErpCurrentStore(){
        List<HashMap<String, Object>> res = currentStockDao.listCurrentStock();
        logger.info("processErpCurrentStore res count = " + res.size());
        int insertCount = 0;
        int updateCount = 0;
        int exceptionCount = 0;
        for (HashMap<String, Object> map : res) {
            try {
                String cWhCode = Util.parseStringFromMap(map, "cWhCode");
                String cWhName = Util.parseStringFromMap(map, "cWhName");
                String cInvCName = Util.parseStringFromMap(map, "cInvCName");
                String cInvName = Util.parseStringFromMap(map, "cInvName");
                String cInvCode = Util.parseStringFromMap(map, "cInvCode");
                String cInvStd = Util.parseStringFromMap(map, "cInvStd");
                BigDecimal iQuantity = new BigDecimal(map.get("iQuantity").toString());
                //String cBatch = Util.parseStringFromMap(map, "cBatch");
                String cBatch = "";
                String cComUnitName = Util.parseStringFromMap(map, "cComUnitName");
                ErpCurrentStore erpCurrentStore = erpCurrentStoreDao.selectByCwhcodeAndInvCode(cWhCode, cInvCode);
                if(erpCurrentStore == null){
                    erpCurrentStore = new ErpCurrentStore();
                    erpCurrentStore.setCwhcode(cWhCode);
                    erpCurrentStore.setCwhname(cWhName);
                    erpCurrentStore.setCinvcname(cInvCName);
                    erpCurrentStore.setCinvname(cInvName);
                    erpCurrentStore.setCinvcode(cInvCode);
                    erpCurrentStore.setCinvstd(cInvStd);
                    erpCurrentStore.setIquantity(iQuantity);
                    erpCurrentStore.setCbatch(cBatch);
                    erpCurrentStore.setCcomunitname(cComUnitName);
                    erpCurrentStore.setBarcodeflag(0);
                    erpCurrentStoreDao.insert(erpCurrentStore);
                    insertCount++;
                }else{
                    boolean isUpdateBasicInfo = false;
                    if(!erpCurrentStore.getCinvname().equals(cInvName)){
                        erpCurrentStore.setCinvname(cInvName);
                        isUpdateBasicInfo=true;
                    }
                    if(!erpCurrentStore.getCcomunitname().equals(cComUnitName)){
                        erpCurrentStore.setCcomunitname(cComUnitName);
                        isUpdateBasicInfo=true;
                    }
                    if(!StrUtil.equals(erpCurrentStore.getCinvstd(), cInvStd, false)){
                        erpCurrentStore.setCinvstd(cInvStd);
                        isUpdateBasicInfo=true;
                    }
                    if(erpCurrentStore.getIquantity().compareTo(iQuantity) != 0){
                        erpCurrentStore.setIquantity(iQuantity);
                        if(isUpdateBasicInfo){
                            erpCurrentStoreDao.updateByPrimaryKey(erpCurrentStore);
                        }else{
                            erpCurrentStoreDao.updateQuantityByPrimaryKey(erpCurrentStore);
                        }
                        updateCount++;
                    }else{
                        if(isUpdateBasicInfo) {
                            erpCurrentStoreDao.updateByPrimaryKey(erpCurrentStore);
                            logger.info(erpCurrentStore.getCinvcode() + " 基础数据发生变更");
                        }
                    }
                }
            }catch (Exception e){
                logger.error(e.getMessage());
                exceptionCount++;
            }
        }
        Etllog etllog = new Etllog("ERP库存现存表", insertCount, updateCount, exceptionCount, 0);
        etllogDao.insert(etllog);
        logger.info("ERP库存现存表新增" + insertCount + "条,修改" + updateCount + "条,异常处理" + exceptionCount + "条,删除" + 0);
        return etllog;
    }
}
